package cn.rengy.web.framework.wxmp.token;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rengy.tpa.wxmp.token.AccessToken;

@Service
public class AccessTokenCacheService {

	private final ConcurrentMap<String, AccessToken> cacheMap = new ConcurrentHashMap<String,AccessToken>(2,1);
	@Autowired
	private TokenService tokenService;
	
	public AccessToken getAccessToken(String appid) {
		if(StringUtils.isBlank(appid)) {
			return null;
		}
		AccessToken accessToken=cacheMap.get(appid);
		if(accessToken==null) {
			//不需要并发控制
			accessToken= new AccessTokenImpl(tokenService,appid);
			cacheMap.put(appid, accessToken);
		}
		return accessToken;
	}
}
